package org.imaginedays.androidall.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * 网络状态变化监听
 * 
 * @author imaginedays
 * 
 */
public class NetWorkChangeListener extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager cMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cMgr.getActiveNetworkInfo();
		int networkType = info.getType();
		showToast(context, getTypeStr(networkType));
	}

	public void showToast(Context context, String msg) {
		Toast.makeText(context, "current network type " + msg,
				Toast.LENGTH_SHORT).show();
	}

	public String getTypeStr(int type) {
		String typeStr = "";
		switch (type) {
		case 0:
			typeStr = "TYPE_MOBILE";
			break;

		case 1:
			typeStr = "TYPE_WIFI";
			break;
		case 2:
			typeStr = "TYPE_MOBILE_MMS";
			break;
		case 3:
			typeStr = "TYPE_MOBILE_SUPL";
			break;
		case 4:
			typeStr = "TYPE_MOBILE_DUN";
			break;
		case 5:
			typeStr = "TYPE_MOBILE_HIPRI";
			break;
		case 6:
			typeStr = "TYPE_WIMAX";
			break;
		case 7:
			typeStr = "TYPE_BLUETOOTH";
			break;
		case 8:
			typeStr = "TYPE_DUMMY";
			break;
		case 9:
			typeStr = "TYPE_ETHERNET";
			break;
		}
		return typeStr;
	}

}
